package com.vgemv.jsutilitysdk.Time2;

import java.text.SimpleDateFormat;
import java.util.Date;

public class JsTimeManager implements JsTimeManagerInterface {
    //标准时间日期字符串
    public  SimpleDateFormat msdf_std;
    //简化的时间日期字符串
    public  SimpleDateFormat msdf_min;

    //日期字符串
    public  SimpleDateFormat mSdf_date;
    //    static {
//        mssdf_std = new SimpleDateFormat();
//        mssdf_std.applyPattern("yyyy-MM-dd HH:mm:ss");
//        mssdf_min = new SimpleDateFormat();
//        mssdf_min.applyPattern("yyyyMMddHHmmss");
//    }
    public static JsTimeManager sharedInstance = new JsTimeManager();


    JsTimeManager() {
        msdf_std = new SimpleDateFormat();
        msdf_std.applyPattern("yyyy-MM-dd HH:mm:ss");
        msdf_min = new SimpleDateFormat();
        msdf_min.applyPattern("yyyyMMddHHmmss");
        mSdf_date = new SimpleDateFormat();
        mSdf_date.applyPattern("yyyy-MM-dd");
    }

    /**
     * 返回标准的FullTime字符串
     * @return "yyyy-MM-dd HH:mm:ss"格式字符串
     */
    public String stdTimeStringForNow() {
        Date d_now = new Date();
        return msdf_std.format(d_now);
    }

    public String minTimeStringForNow() {
        Date d_now = new Date();
        return msdf_min.format(d_now);
    }

    //获取东8区的今天0点的标准时间字符串,究竟是Date创建的是当前时区的date??
    public String stdTimeStringForTodayZero() {
        Date d_now = new Date();
        // 每天的毫秒数
        long l = 24*60*60*1000;
        long l_toayZero = d_now.getTime() - d_now.getTime()%l - 8*60*60*1000;
        Date d_todayZero = new Date(l_toayZero);
        return msdf_std.format(d_todayZero);
    }

    //获取东8区明天0点的标准时间字符串,这个时间居然是8点钟!,也就是说,自动调整了时区!!!
    public String stdTimeStringForTomorrowZero() {
        Date d_now = new Date();
        long l = 24*60*60*1000;
        long l_tomorrowZero = d_now.getTime() - d_now.getTime()%l + l - 8*60*60*1000;
        Date d_tomorrowZero = new Date(l_tomorrowZero);
        return msdf_std.format(d_tomorrowZero);
    }

    public String timeStringForNow() {
        Date d_now = new Date();
        return msdf_std.format(d_now);
    }

//    public static String timeStringForNow() {
//        Date d_now = new Date();
//        return mssdf.format(d_now);
//    }


    @Override
    public String stdTimeStringInmmssForMiniSeconds(int miniSeconds) {
        int seconds = miniSeconds / 1000;
        return stdTimeStringInmmssForSeconds(seconds);
    }

    public String stdTimeStringInmmssForSeconds(int seconds) {
        int min = seconds/60;
        int sec = seconds - min * 60;
        String result = String.format("%02d:%02d",min,sec);
        return result;
    }

    public Date dateFromStdTimeString(String timeString) {
        Date date = null;
        try {
            date = msdf_std.parse(timeString);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public String descriptinForStdTimeStringDifNow(String timeString) {
        Date date_history = dateFromStdTimeString(timeString);
        Date date_now = new Date();
        //秒的差数
        long interval = (date_now.getTime() - date_history.getTime())/1000;

        long min = (interval)/60;
        long sec = (interval)%60;

        if(min == 0) {
            if(sec < 10) {
                //小于10秒.
                return "刚刚";
            }
            else {
                //小于1分钟
                return String.format("%d秒前",sec);
            }
        }
        else {
            //大于1分钟

            if(min<60) {
                return String.format("%d分钟前",min);
            }
            else {
                //大于1h
                long hour = min/60;

                if(hour<24) {
                    return String.format("%d小时前",hour);
                }
                else {
                    //大于1天
                    long day = hour/24;
                    if(day<3) {
                        return String.format("%d天前",day);
                    }
                    else {
                        //大于3天,显示MM-dd
                        return timeString.substring(5,10);
                    }
                }

            }
        }

    }

    // 判断当前时间是否为今日
    public boolean isStdDateTimeStringInToday(String dateTimeString) {
        String dateString = dateTimeString.substring(0,10);
        return isStdDateStringInToday(dateString);
    }
    public boolean isStdDateStringInToday(String dateString) {
        Date now = new Date();
        String string = mSdf_date.format(now);
        if(string.equals(dateString)) {
            return true;
        }
        else {
            return false;
        }
    }
}
